<?php

namespace app\system\controller;

use app\common\model\CmsCategoryModel;
use app\common\model\ShopGoodsCategoryModel;
use app\common\model\SiteClassModel;
use think\Request;

class SiteClassController extends BaseController
{
    private $siteClassCache;
    private $siteClassModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->siteClassModel = new SiteClassModel();
        $this->siteClassCache = cache('site_class');
    }

    /**
     * 分类首页
     * @return mixed
     */
    public function index()
    {
        $this->assign('list', $this->siteClassCache);
        return $this->fetch();
    }

    /**
     * 添加行业
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, [
                'site_class_name|分类名称'=>'require',
                'bind_domain|绑定域名'=>'url',
                'bind_goods_cid|绑定类目'=>'>:0',
                'bind_cms_cid|绑定栏目'=>'>:0',
            ]);
            if ($result !== true) {
                $this->error($result);
            }
            if ($this->siteClassModel->addData($data)) {
                $this->siteClassModel->updateCache();
                $this->success('添加成功！', url('index'));
            } else {
                $this->error($this->siteClassModel->getError());
            }
        }
        $this->assign('category',$this->getCategory());
        return $this->fetch();
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, [
                'site_class_name|分类名称'=>'require',
                'bind_domain|绑定域名'=>'url',
                'bind_goods_cid|绑定类目'=>'>:0',
                'bind_cms_cid|绑定栏目'=>'>:0',
            ]);
            if ($result !== true) {
                $this->error($result);
            }
            $this->siteClassModel->editData($data);
            $this->siteClassModel->updateCache();
            $this->success('操作成功！', url('index'));
        }
        $siteClassId = $this->request->param('site_class_id');
        $info    = $this->siteClassCache[$siteClassId];
        $this->assign('category',$this->getCategory());
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del()
    {
        $siteClassId   = $this->request->param('site_class_id');
        $this->siteClassModel->where('site_class_id',$siteClassId)->delete();
        $this->siteClassModel->updateCache();
        $this->success('操作成功！');
    }

    /**
     * 更新排序
     */
    public function updateOrder()
    {
        $listOrder = $this->request->param('list_order/a');
        foreach ($listOrder as $site_class_id => $site_class_sort) {
            $site_class_id    = intval($site_class_id);
            $site_class_sort  = intval($site_class_sort);
            $dataList[] = ["site_class_id" => $site_class_id, "site_class_sort" => $site_class_sort];
        }
        $this->siteClassModel->saveAll($dataList);
        $this->updateCache();
        $this->success('排序成功！');
    }

    /**
     * 更新缓存
     */
    public function updateCache()
    {
        if ($this->siteClassModel->updateCache()) {
            $this->success('更新成功！');
        }
    }

    /**
     * 获取绑定类目
     */
    private function getCategory(){
        $goodsCategoryModel = new ShopGoodsCategoryModel();
        $cmsCategoryModel = new CmsCategoryModel();
        $data['goods_category'] = $goodsCategoryModel->where('pid',0)->select()->toArray();
        $data['cms_category'] = $cmsCategoryModel->where('pid',0)->select()->toArray();
        return $data;
    }

}
